package response

import (
	"fmt"
	"github.com/gogf/gf/v2/errors/gcode"
)

type ResCode struct {
	code    int
	message string
	detail  interface{}
}

func (r ResCode) Code() int {
	return r.code
}

func (r ResCode) Message() string {
	return r.message
}

func (r ResCode) Detail() interface{} {
	return r.detail
}

func (r ResCode) String() string {
	if r.detail != nil {
		return fmt.Sprintf("%d:%s %v", r.code, r.message, r.detail)
	}
	if r.message != "" {
		return fmt.Sprintf("%d:%s", r.code, r.message)
	}
	return fmt.Sprintf("%d", r.code)
}

var (
	CodeSuccess    = New(0, "Success", "")
	CodeFail       = New(1, "Fail", "")
	CodeBadRequest = New(400, "Bad Request", "")
	CodeNotFound   = New(404, "Not Found", "")
	CodeInternal   = New(500, "Internal Error", "")
)

func New(code int, message string, detail interface{}) gcode.Code {
	return ResCode{
		code:    code,
		message: message,
		detail:  detail,
	}
}
